import routers from "../routers";

type RouteObject = {
  path: string;
  element: React.ReactNode;
  children?: RouteObject[];
  name: string;
};

function findRouteByPath(routes: RouteObject[]|any, path: string): RouteObject | undefined {
  for (const route of routes) {
    if (route.path === path) {
      return route;
    }

    if (route.children && route.children.length > 0) {
      const childRoute = findRouteByPath(route.children, path);
      if (childRoute !== undefined) {
        return childRoute;
      }
    }
  }

  return undefined;
}

export default function getRouteByPath(path: string): RouteObject | undefined {
  return findRouteByPath(routers.routes, path);
}